<?php

namespace app\user\controller\mall\goods;

use app\user\controller\mall\Base;

/**
 * 商品评价
 *
 * @icon fa fa-circle-o
 */
class Appraise extends Base
{
    
    /**
     * Appraise模型对象
     * @var \app\common\model\mall\goods\Appraise
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\goods\Appraise;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function index() {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->where('shop_id',cookie('shop_id'))
                ->where("status","<>",'')
                ->count();

            $list = $this->model
                ->where($where)
                ->where('shop_id',cookie('shop_id'))
                ->where("status","<>",'')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->alias("a")
            ->join("__MALL_ORDER_GOODS__ g","a.ordergoods_id=g.id and a.order_id=g.order_id and a.goods_id=g.goods_id and a.goodsspec_id=g.salespec_id","LEFT")
            ->join('__MALL_ORDER_ORDER__ o','a.order_id = o.id and a.shop_id=o.shop_id','LEFT')
            ->join("__MALL_GOODS_GOODS__ gg","a.goods_id=gg.id","LEFT")
            ->field('a.*,g.order_id,g.goods_id,g.goodsPrice,g.goodsSpecNames,gg.goodsname,gg.goodsimg,g.salespec_id,g.goodsCode,o.orderNo')
            ->where("a.id",$ids)->find();
        if (!$row) $this->error(__('No Results were found'));
        //$row['user_id_text'] = $row->user_id_text;
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            $params['replytime'] = date('Y-m-d H:i:s');
            if ($params){
                try{
                    //是否采用模型验证
                    if ($this->modelValidate){
                        $name = basename(str_replace("\\model\\", "\\validate\\", get_class($this->model)));
                        $validate = $this->modelSceneValidate ? $name . '.edit' : $name;
                        $result = $this->validate($params,$validate);
                        if(true !== $result){
                            // 验证失败 输出错误信息
                            $this->error($result);
                        }
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false){
                        $this->success();
                    }
                    else{
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e){
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


}
